package algorithm;

import java.util.Arrays;
import java.util.Comparator;

public class leetcode_0406 {
    public static void main(String[] args) {
        int[][] people={{9,0},{7,0},{1,9},{3,0},{2,7},{5,3},{6,0},{3,4},{6,2},{5,2}};
        reconstructQueue(people);
    }
    public static int[][] reconstructQueue(int[][] people) {
        int len= people.length;
        Arrays.sort(people, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                if(o2[0]==o1[0]){
                    return o1[1]-o2[1];
                }
                else return o2[0]-o1[0];
            }
        });
        for(int i=0;i<len;++i){
            if(i!=people[i][1]){
                int [] tem=new int[2];
                tem[0]=people[i][0];
                tem[1]=people[i][1];
                for(int k=i;k>tem[1];--k){
                    people[k][0]=people[k-1][0];
                    people[k][1]=people[k-1][1];
                }
                people[tem[1]][0]=tem[0];
                people[tem[1]][1]=tem[1];
            }
        }
        return people;
    }
}
